package mobilizator.app.list;

import java.util.ArrayList;

import mobilizator.app.R;
import mobilizator.app.data.Category;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ClothCategoriesPagerAdapter extends PagerAdapter {

	private Context ctx;
	private ArrayList<Category> categoriesMain;
	private ArrayList<Category> categoriesUser;
	private ListView listView;
	private View view;

	public ClothCategoriesPagerAdapter(Context ctx,
			ArrayList<Category> categoriesMain,
			ArrayList<Category> categoriesUser) {
		this.ctx = ctx;
		this.categoriesMain = categoriesMain;
		this.categoriesUser = categoriesUser;
	}

	@Override
	public int getCount() {
		return 2;
	}

	public Object instantiateItem(View collection, int position) {
		LayoutInflater inflater = (LayoutInflater) collection.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		switch (position) {
		case 0:
			view = inflater.inflate(R.layout.cloth_list_categories_main, null);
			listView = (ListView) view.findViewById(R.id.cloth_list_categories_main_listView);
			listView.setAdapter(new ClothCategoriesAdapter(ctx, categoriesMain));
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int pos, long arg3) {
					final Intent clothList = new Intent(ctx,
							ClothListActivity.class);
					clothList.putExtra("category", categoriesMain.get(pos)
							.getId());
					ctx.startActivity(clothList);
				}
			});
			Log.d("ilosc_kat_main",
					new Integer(categoriesMain.size()).toString());
			break;
		case 1:
			view = inflater.inflate(R.layout.cloth_list_categories_user, null);
			listView = (ListView) view.findViewById(R.id.cloth_list_categories_user_listView);
			listView.setAdapter(new ClothCategoriesAdapter(ctx, categoriesUser));
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int pos, long arg3) {
					final Intent clothList = new Intent(ctx,
							ClothListActivity.class);
					clothList.putExtra("category", categoriesUser.get(pos)
							.getId());
					ctx.startActivity(clothList);
				}
			});
			Log.d("ilosc_kat_user",
					new Integer(categoriesUser.size()).toString());
			break;
		}

		((ViewPager) collection).addView(view, 0);
		return view;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (View) arg1;
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

}
